package com.region.http.client.config;

import com.region.common.adapter.json.JSONAdapter;
import com.region.common.adapter.logging.LoggerAdapter;
import com.region.loadbalancer.LoadBalancerConfig;

import java.util.LinkedList;
import java.util.List;

/**
 * The http client default config
 *
 * @author liujieyu
 * @date 2023/6/4 22:16
 * @desciption
 */
public class ClientConfig {

    /**
     * The scanning packages
     */
    private String[] basePackages;

    /**
     * Set Logger framework Adapter
     */
    private Class<? extends LoggerAdapter> loggerAdapter;

    /**
     * Set Json framework adapter
     */
    private Class<? extends JSONAdapter> jsonAdapter;
    /**
     * The load balancer config
     */
    private LoadBalancerConfig loadBalancerConfig;

    /**
     * The Temp Cache Path
     */
    private String cachePath;

    /**
     * The environment resource path
     */
    private List<String> resources;

    /**
     * Global request config
     */
    private GlobalRequestConfig globalRequestConfig;

    /**
     * Automatically monitors the load balancer
     */
    private boolean autoMonitor;

    private ClientConfig(String[] basePackages, Class<? extends LoggerAdapter> loggerAdapter, Class<? extends JSONAdapter> jsonAdapter,
                         LoadBalancerConfig loadBalancerConfig, String cachePath, List<String> resources, GlobalRequestConfig globalRequestConfig, boolean autoMonitor) {
        this.basePackages = basePackages;
        this.loggerAdapter = loggerAdapter;
        this.jsonAdapter = jsonAdapter;
        this.loadBalancerConfig = loadBalancerConfig;
        this.cachePath = cachePath;
        this.resources = resources;
        this.globalRequestConfig = globalRequestConfig;
        this.autoMonitor = autoMonitor;
    }

    public String[] getBasePackages() {
        return basePackages;
    }

    public Class<? extends LoggerAdapter> getLoggerAdapter() {
        return loggerAdapter;
    }

    public Class<? extends JSONAdapter> getJsonAdapter() {
        return jsonAdapter;
    }

    public LoadBalancerConfig getLoadBalancerConfig() {
        return loadBalancerConfig;
    }

    public String getCachePath() {
        return cachePath;
    }

    public List<String> getResources() {
        return resources;
    }

    public GlobalRequestConfig getGlobalRequestConfig() {
        return globalRequestConfig;
    }

    public boolean isAutoMonitor() {
        return autoMonitor;
    }

    public static ClientConfigBuilder builder() {
        return new ClientConfigBuilder();
    }

    public static final class ClientConfigBuilder {
        /**
         * The scanning packages
         */
        private String[] basePackages;

        /**
         * Set Logger framework Adapter
         */
        private Class<? extends LoggerAdapter> loggerAdapter;

        /**
         * Set Json framework adapter
         */
        private Class<? extends JSONAdapter> jsonAdapter;
        /**
         * The load balancer config
         */
        private LoadBalancerConfig loadBalancerConfig = new LoadBalancerConfig();

        /**
         * The Temp Cache Path
         */
        private String cachePath = System.getProperty("java.io.tmpdir");

        /**
         * The environment resource path
         */
        private List<String> resources = new LinkedList<>();

        /**
         * Global request config
         */
        private GlobalRequestConfig globalRequestConfig;

        /**
         * Automatically monitors the load balancer, default true
         */
        private boolean autoMonitor = true;

        private ClientConfigBuilder() {
            super();
        }

        public ClientConfigBuilder basePackages(String[] basePackages) {
            this.basePackages = basePackages;
            return this;
        }

        public ClientConfigBuilder loggerAdapter(Class<? extends LoggerAdapter> loggerAdapter) {
            this.loggerAdapter = loggerAdapter;
            return this;
        }

        public ClientConfigBuilder jsonAdapter(Class<? extends JSONAdapter> jsonAdapter) {
            this.jsonAdapter = jsonAdapter;
            return this;
        }

        public ClientConfigBuilder loadBalancerConfig(LoadBalancerConfig loadBalancerConfig) {
            this.loadBalancerConfig = loadBalancerConfig;
            return this;
        }

        public ClientConfigBuilder cache(String path) {
            this.cachePath = path;
            return this;
        }

        public ClientConfigBuilder resource(String resource) {
            resources.add(resource);
            return this;
        }

        public ClientConfigBuilder globalRequestConfig(GlobalRequestConfig globalRequestConfig) {
            this.globalRequestConfig = globalRequestConfig;
            return this;
        }

        public ClientConfigBuilder autoMonitor(boolean autoMonitor) {
            this.autoMonitor = autoMonitor;
            return this;
        }

        public ClientConfig build() {
            if (this.globalRequestConfig == null) {
                this.globalRequestConfig = GlobalRequestConfig.builder().build();
            }
            ClientConfig clientConfig = new ClientConfig(basePackages, loggerAdapter, jsonAdapter, loadBalancerConfig, cachePath, resources, globalRequestConfig, autoMonitor);
            return clientConfig;
        }
    }

}
